{% import 'macros/form.html' as form %}

{% set default_group_type = h.default_group_type('organization') %}

{% block package_basic_fields_title %}
  {{ form.input('title', id='field-title', label=_('Title'), placeholder=_('eg. A descriptive title'), value=data.title, error=errors.title, classes=['control-full', 'control-large'], attrs={'data-module': 'slug-preview-target', 'class': 'form-control'}) }}
{% endblock %}

{% block package_basic_fields_url %}
  {% set prefix = h.url_for('dataset.read', id='') %}
  {% set domain = h.url_for('dataset.read', id='', qualified=true) %}
  {% set domain = domain|replace("http://", "")|replace("https://", "") %}
  {% set attrs = {'data-module': 'slug-preview-slug', 'data-module-prefix': domain, 'data-module-placeholder': '<dataset>', 'class': 'form-control input-sm'} %}

  {{ form.prepend('name', id='field-name', label=_('URL'), prepend=prefix, placeholder=_('eg. my-dataset'), value=data.name, error=errors.name, attrs=attrs, is_required=true) }}
{% endblock %}

{% block package_basic_fields_custom %}{% endblock %}

{% block package_basic_fields_description %}
  {{ form.markdown('notes', id='field-notes', label=_('Description'), placeholder=_('eg. Some useful notes about the data'), value=data.notes, error=errors.notes) }}
{% endblock %}

{% block package_basic_fields_tags %}
  {% set tag_attrs = {'data-module': 'autocomplete', 'data-module-tags': '', 'data-module-source': '/api/2/util/tag/autocomplete?incomplete=?'} %}
  {{ form.input('tag_string', id='field-tags', label=_('Tags'), placeholder=_('eg. economy, mental health, government'), value=data.tag_string, error=errors.tags, classes=['control-full'], attrs=tag_attrs) }}
{% endblock %}

{% block package_basic_fields_license %}
  <div class="form-group">
    {% set error = errors.license_id %}
    <label class="control-label" for="field-license">{{ _("License") }}</label>
    <div class="controls">
      <div class="row">
        <div class="col-md-6">
          <select id="field-license" name="license_id" data-module="autocomplete">
            {% set existing_license_id = data.get('license_id') %}
            {% set empty_license = _('Please select the license') %}
            <option value="">{{ empty_license }}</option>
            {% for license_id, license_desc in h.license_options(existing_license_id) %}
              <option value="{{ license_id }}" {% if existing_license_id == license_id %}selected="selected"{% endif %}>{{ license_desc }}</option>
            {% endfor %}
          </select>
          {% if error %}<span class="error-block">{{ error }}</span>{% endif %}
        </div>
        <div class="col-md-6">
          <span class="info-block info-inline">
            <i class="fa fa-info-circle"></i>
            {% trans %}
            License definitions and additional information can be found
            at <a href="http://opendefinition.org/licenses/">opendefinition.org</a>
            {% endtrans %}
          </span>
        </div>
      </div>
    </div>
  </div>
{% endblock %}

{% block package_basic_fields_org %}
  {# if we have a default group then this wants remembering #}
  {% if data.group_id %}
    <input type="hidden" name="groups__0__id" value="{{ data.group_id }}" />
  {% endif %}

  {% set dataset_is_draft = data.get('state', 'draft').startswith('draft') or data.get('state', 'none') ==  'none' %}
  {% set dataset_has_organization = data.owner_org or data.group_id %}
  {% set organizations_available = h.organizations_available('create_dataset') %}
  {% set user_can_update_owner_org = h.can_update_owner_org(data, organizations_available) %}
  {% set user_is_sysadmin = h.check_access('sysadmin') %}
  {% set show_organizations_selector = organizations_available and user_can_update_owner_org %}
  {% set show_visibility_selector = dataset_has_organization or (organizations_available and (user_is_sysadmin or dataset_is_draft)) %}

  {% if show_organizations_selector and show_visibility_selector %}
    <div data-module="dataset-visibility">
  {% endif %}

  {% if show_organizations_selector %}
    {% set existing_org = data.owner_org or data.group_id %}
    <div class="form-group control-medium">
      <label for="field-organizations" class="control-label">{{ h.humanize_entity_type('organization', default_group_type, 'default label') or _('Organization') }}</label>
      <div class="controls">
        <select id="field-organizations" name="owner_org" data-module="autocomplete">
          {% if h.check_config_permission('create_unowned_dataset') %}
            <option value="" {% if not selected_org and data.id %} selected="selected" {% endif %}>{{ h.humanize_entity_type('organization', default_group_type, 'no label') or _('No Organization') }}</option>
          {% endif %}
          {% for organization in organizations_available %}
            {# get out first org from users list only if there is not an existing org #}
          {% set selected_org = (existing_org and existing_org == organization.id) or (not existing_org and not data.id and organization.id == organizations_available[0].id) %}
          <option value="{{ organization.id }}" {% if selected_org %} selected="selected" {% endif %}>{{ organization.display_name }}</option>
          {% endfor %}
        </select>
      </div>
    </div>
    {% endif %}

    {% if show_visibility_selector %}
    {% block package_metadata_fields_visibility %}
    <div class="form-group control-medium">
      <label for="field-private" class="control-label">{{ _('Visibility') }}</label>
      <div class="controls">
        <select id="field-private" name="private" class="form-control">
          {% for option in [('True', _('Private')), ('False', _('Public'))] %}
          <option value="{{ option[0] }}" {% if option[0] == data.private|trim %}selected="selected"{% endif %}>{{ option[1] }}</option>
          {% endfor %}
        </select>
      </div>
    </div>
    {% endblock %}
    {% endif %}

    {% if show_organizations_selector and show_visibility_selector %}
  </div>
  {% endif %}


  {% if data.id and h.check_access('package_delete', {'id': data.id}) and data.state != 'active' %}
  <div class="form-group control-medium">
    <label for="field-state" class="control-label">{{ _('State') }}</label>
    <div class="controls">
      <select class="form-control" id="field-state" name="state">
        <option value="active" {% if data.get('state', 'none') == 'active' %} selected="selected" {% endif %}>{{ _('Active') }}</option>
        <option value="deleted" {% if data.get('state', 'none') == 'deleted' %} selected="selected" {% endif %}>{{ _('Deleted') }}</option>
      </select>
    </div>
  </div>
  {% endif %}

  {% endblock %}
